package com.start.workflow.core;

import com.alibaba.fastjson2.JSONArray;
import com.start.avue.AvueBaseFormColumn;
import com.start.form.engine.core.model.StartFormColumnModel;
import com.start.form.engine.core.entity.StartFormConfig;
import com.start.form.engine.core.listener.StartTableListener;
import com.start.form.engine.core.tools.StartAvueColTools;
import com.start.form.engine.core.tools.StartTableOptionTools;
import com.start.workflow.common.StartActBusForm;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.start.form.engine.core.StartFormEngineConstant.FormBaseColumns.COLUMN_PK_NAME;
import static com.start.workflow.common.StartActBusForm.ProcCol.*;
import static com.start.workflow.common.StartActBusForm.ProcTaskCol.*;

@Component
public class StartActFormListener implements StartTableListener {
    private static final Map<String, String> TABLE_NAME_MAP = new HashMap<>();

    static {
        TABLE_NAME_MAP.put(StartActFormType.START_ACT_PROC.getValue(), "start_act_bus_form_");
        TABLE_NAME_MAP.put(StartActFormType.START_ACT_NODE.getValue(), "start_act_bus_form_task_");
    }

    /**
     * 获取需要监听的数据表所属组别 多个请用英文逗号隔开
     *
     * @return
     */
    @Override
    public String getGroupCode() {
        return StartActFormType.START_ACT_PROC.getValue() + "," + StartActFormType.START_ACT_NODE.getValue();
    }

    /**
     * 创建数据表前
     *
     * @param formConfig
     */
    @Override
    public void beforeCreateTable(StartFormConfig formConfig) {
        checkTableName(formConfig);
        autoAppendFormFields(formConfig);
    }

    private void autoAppendFormFields(StartFormConfig formConfig) {
        JSONArray column = StartTableOptionTools.getColumn(formConfig.getFormOption());
        Map<String, Integer> map = StartTableOptionTools.getTableOptionFieldIndex(column);
        StartAvueColTools.autoAppendField(map, column, PROCESS_DEFINITION_KEY, "主键", "varchar", 60);
        StartAvueColTools.autoAppendField(map, column, PROCESS_INSTANCE_ID, "流程实例id", "varchar", 60);
        StartAvueColTools.autoAppendField(map, column, PROCESS_STATE, "流程状态", "int");
        if (!formConfig.getFormCategory().equals(StartActFormType.START_ACT_NODE.getValue())) {
            return;
        }
        StartAvueColTools.autoAppendField(map, column, PROCESS_TASK_DEF_KEY, "流程任务定义key", "varchar", 60);
        StartAvueColTools.autoAppendField(map, column, PROCESS_TASK_STATE_KEY, "流程任务定义key", "任务审批状态", 60);
        StartAvueColTools.autoAppendField(map, column, PROCESS_TASK_INST_ID, "流程任务定义key", "流程任务实例id", 60);

    }


    /**
     * 更新数据表前
     *
     * @param formConfig 表新配置
     * @param oldColumn  表旧列
     */
    @Override
    public void beforeUpdateTable(StartFormConfig formConfig, List<StartFormColumnModel> oldColumn) {
        checkTableName(formConfig);
        autoAppendFormFields(formConfig);
    }

    private void checkTableName(StartFormConfig formConfig) {
        String formCategory = formConfig.getFormCategory();
        String form_prefix = TABLE_NAME_MAP.get(formCategory);
        String tableName = formConfig.getTableName().trim();
        if (tableName.startsWith("_")) {
            tableName = tableName.substring(1);
        }
        if (tableName.startsWith(form_prefix)) {
            return;
        }
        String trim = formConfig.getTableName().trim();
        formConfig.setTableName(form_prefix + trim);

    }
}
